package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannerMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannerMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByPageAndName(ChannelDto dto) {

        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //封装查询条件
        //分页条件
        Page pageResult = new Page<>(dto.getPage(), dto.getSize());
        //查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }

        //分页查询
        IPage result = page(pageResult, queryWrapper);

        //封装分页查询结果返回

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());

        System.out.println("pageResponseResult = " + pageResponseResult.getCurrentPage());
        System.out.println("pageResponseResult = " + pageResponseResult.getTotal());

        pageResponseResult.setData(result.getRecords());

        return pageResponseResult;
    }

    /**
     * 新增频道
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //判断参数
        if (channel == null) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //执行添加
        save(channel);

        //返回封装后的结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updates(AdChannel channel) {
        //判断参数
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //修改频道
        updateById(channel);

        //返回封装后的结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //TODO:自定义异常
//        if(id >10){
//            throw new CustomException(ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能大于10"));
//        }

        //删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
